package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sy.simpleegg.bean.GoodBean
import com.sy.simpleegg.config.Config
import com.sy.simpleegg.databinding.ItemPkgForeignBinding
import com.sy.simpleegg.util.BigDecimalUtil

class GoodForeignAdapter(
    private var mContext: Context,
    private var mGoodList: MutableList<GoodBean>
) : RecyclerView.Adapter<GoodForeignAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodForeignAdapter.ViewHolder {
        val binding = ItemPkgForeignBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("NewApi", "SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: GoodForeignAdapter.ViewHolder, position: Int) {
        val goodBean: GoodBean = mGoodList[position]

        holder.coins.text = goodBean.amount
        holder.price.text = "${BigDecimalUtil.getPrice(goodBean.money)}"
        holder.priceSymbol.text = Config.CURRENCY_SYMBOLS

        holder.rlGood.setOnClickListener {
            mOnClickListener?.onClick(goodBean)
        }
    }

    override fun getItemCount(): Int {
        return mGoodList.size
    }

    inner class ViewHolder(binding: ItemPkgForeignBinding) : RecyclerView.ViewHolder(binding.root) {
        val coins: TextView = binding.tvCoins
        val price: TextView = binding.tvPrice
        val priceSymbol: TextView = binding.tvPriceUnit
        val rlGood: RelativeLayout = binding.rlGood
    }


    fun setData(newList: MutableList<GoodBean>?) {
        if (newList.isNullOrEmpty()) {
            return
        }
        mGoodList = newList
        notifyDataSetChanged()
    }


    interface OnItemClickListener {
        fun onClick(goodBean: GoodBean?)
    }

    private var mOnClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnClickListener = onItemClickListener
    }
}